package com.kkd.kkdapp.db.app.model;

import java.util.List;

import android.content.ContentValues;

import com.kkd.kkdapp.db.annotate.EADBField;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldMode;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldType;
import com.kkd.kkdapp.db.app.EADBHelper;
import com.kkd.kkdapp.db.base.EABaseModel;
import com.kkd.kkdapp.db.user.KkdDBHelper;
import com.kkd.kkdapp.http.entity.HomePageNotice;
import com.kkd.kkdapp.http.entity.InformationDataList;

/**
 * ClassName: KkdMessageModel <br/>
 * date: 2016-5-25 下午2:54:50 <br/>
 * 首页公告消息
 * @author askzhao
 * @version 
 */
public class Notice extends EABaseModel {

	public static final String TABLE_NAME = Notice.class.getSimpleName();
	public static final int CONTAINED = -2016;//表明已经存在该消息
	
	@EADBField(mode = EADBFieldMode.NotNull,type = EADBFieldType.Text )
	public String id;
	@EADBField(type = EADBFieldType.Text)
	public String title;
	@EADBField(type = EADBFieldType.Text)
	public String publishDate;
	@EADBField(type = EADBFieldType.Text)
	public String description;
	@EADBField(type = EADBFieldType.Text)
	public String status = "未读";
	@EADBField(type = EADBFieldType.Integer)
	public long createTime;
	

	public static long insert(InformationDataList msg) {
		Notice curMsg = EADBHelper.getInstance().queryTopOne(Notice.class, " id=? ",
				new String[] { msg.getId() });
		if(curMsg!=null){
			return CONTAINED;
		}
		Notice newMsg = new Notice();		
		newMsg.id = msg.getId();
		newMsg.title = msg.getTitle();
		newMsg.publishDate = msg.getPublishDate();
		newMsg.description = msg.getDescription();
		if(msg.getStatus()==null){
			newMsg.status = "未读";
		}else{
			newMsg.status = msg.getStatus();
		}
		
		newMsg.createTime = msg.getCreateTime();
		return EADBHelper.getInstance().saveBindId(newMsg);
	}
	
	public static void insertNotices(List<InformationDataList> homePageNotices) {
		
		for(InformationDataList h:homePageNotices){
			insert(h);
		}
	}

	public static List<Notice>  queryAllNotice() {
		List<Notice> mNotices = null;
		if(EADBHelper.getInstance()!=null){
			mNotices = EADBHelper.getInstance().queryAll(Notice.class, " status=? ", new String[] { "未读" },  " id asc ", null);
		}
		return mNotices;
	}
	
	public static boolean updateStatusById(String status,String id) {
		ContentValues values = new ContentValues();
		values.put("status", status);
		return EADBHelper.getInstance().update(TABLE_NAME, values, " id=? ",
				new String[] { id }) > 0;
	}

	public static void clearNotice() {
		EADBHelper.getInstance().delete(TABLE_NAME, null, null);
		
	}
}
